//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by Fernflower decompiler)
//

package com.comprosoft.telnet.server;

import com.comprosoft.telnet.command.AllCommands;
import com.comprosoft.telnet.command.Command;
import com.comprosoft.telnet.io.IOHandler;
import com.comprosoft.telnet.io.TextColor;
import java.io.IOException;
import java.net.Socket;
import java.util.logging.Level;
import java.util.logging.Logger;

public class ClientWorker implements Runnable {
    private Socket socket;
    private IOHandler io;
    private Logger logger = Logger.getLogger(ClientWorker.class.getName());
    private int maxWaitTime;
    volatile long startTime = 0L;
    private TelnetServer telnetServer;

    public ClientWorker(Socket socket, String homeDir, TelnetServer telnetServer) throws IOException {
        this.socket = socket;
        this.io = new IOHandler(socket);
        this.maxWaitTime = 100000;
        this.telnetServer = telnetServer;
    }

    public void run() {
        try {
            this.showWelcomeScreen();
            Thread thread = Thread.currentThread();
            boolean cancel = false;
            this.startTime = System.currentTimeMillis();
            boolean usernameFilled = false;
            boolean passwordFilled = false;

            while(!cancel && !thread.isInterrupted()) {
                String command = null;
                if (!usernameFilled) {
                    this.io.SetTextColor(TextColor.YELLOW);
                    command = this.io.readLine("username: ");
                    command = "username " + command;
                } else if (!passwordFilled) {
                    this.io.SetTextColor(TextColor.YELLOW);
                    command = this.io.readLine("password: ");
                    command = "password " + command;
                } else {
                    this.io.SetTextColor(TextColor.WHITE);
                    command = this.io.readLine("-> ");
                }

                command = command.trim();
                if (command == null) {
                    this.io.WriteLine("Null!");
                } else if (command.length() != 0) {
                    String[] tokens = Command.tokenize(command);
                    Command cmd = AllCommands.getCommand(tokens[0], this.io, this.telnetServer);
                    switch(cmd.handle(tokens)) {
                    case COMMAND_EXIT:
                        cancel = true;
                        break;
                    case COMMAND_RESPLASH:
                        this.io.ClearScreen();
                        this.showWelcomeScreen();
                        break;
                    case COMMAND_FILLED_USERNAME:
                        usernameFilled = true;
                        this.io.WriteLine("");
                        this.io.WriteLine("");
                        break;
                    case COMMAND_FILLED_PASSWORD:
                        passwordFilled = true;
                        this.io.WriteLine("");
                        this.io.WriteLine("");
                    }

                    this.startTime = System.currentTimeMillis();
                }
            }
        } catch (IOException var16) {
            this.io.SetTextColor(TextColor.RED);
            this.io.WriteLine(var16.getMessage());
            this.logger.log(Level.SEVERE, var16.getMessage(), var16);
        } finally {
            try {
                this.io.WriteLine("\ntelnet link has been closed");
                this.io.Flush();
                this.io.destroy();
                this.socket.close();
            } catch (IOException var15) {
                this.logger.log(Level.SEVERE, "Failed to close the socket", var15);
            }

        }

    }

    private void showWelcomeScreen() {
        this.io.SetTextColor(TextColor.WHITE);
        this.io.WriteLine("");
        this.io.WriteLine("======================================================");
        this.io.WriteLine("");
        this.io.WriteLine("   Welcome to Comprosoft Telnet Server: Version 1.0   ");
        this.io.WriteLine("");
        this.io.WriteLine("======================================================");
        this.io.WriteLine("");
        this.io.WriteLine("");
        this.io.SetTextColor(TextColor.CYAN);
        this.io.Write("Type ");
        this.io.SetTextColor(TextColor.MAGENTA);
        this.io.Write("\"List\"");
        this.io.SetTextColor(TextColor.CYAN);
        this.io.WriteLine(" to show all available commands.");
        this.io.WriteLine("");
        this.io.WriteLine("");
    }

    public static void main(String[] args) {
        String ss = "sadsadsa张";
        System.out.println(ss.replaceAll("\\P{Print}", ""));
    }
}
